package com.zsqx.tools;

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

public class SMSTools {
	
	static Logger logger = Logger.getLogger(SMSTools.class.getName());
	//106570214112104信灵移动测试端口,只能发不能收
	//调用68的接收短信接口为气象局的接口
	public static final String WS_URL = "http://202.105.179.68:6088/Service.asmx";
	public static final String namespace = "http://tempuri.org/";
	public static final String receiveMethod = "myReceiveSMS";
	public static final String sendMethod = "mySendSMS";
/**
 * 接收短信
 */
	public static void myReceiveSMS(){
		parseReceiveResponse();
	}
	/**
	 * 发送短信
	 * @param mobiles
	 * @param content
	 * @return
	 */
	public static boolean mySendSMS(String mobiles,String content){
		boolean flag=false;
		String str=getSendResporse(mobiles,content);
		if ("SUC".equalsIgnoreCase(str)) {
			flag=true;
		}
		return flag;
	}
	static String getSendResporse(String mobiles,String content){
		String res="";
		try {
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setOperationName(new QName(namespace,sendMethod));
			call.addParameter(new QName(namespace,"mobiles"),XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter(new QName(namespace,"content"),XMLType.XSD_STRING, ParameterMode.IN);
			call.setSOAPActionURI(namespace+sendMethod);
			call.setUseSOAPAction(true);
			call.setReturnType(XMLType.XSD_STRING);
			call.setTargetEndpointAddress(new java.net.URL(WS_URL));
			res=(String) call.invoke(new Object[]{mobiles,content});
			System.out.println("sending response:"+res);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}
	static String getReceiveResponse(){
		String res="";
		try {
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setOperationName(new QName(namespace,receiveMethod));
			call.setSOAPActionURI(namespace+receiveMethod);
			call.setUseSOAPAction(true);
			call.setReturnType(XMLType.XSD_STRING);
			call.setTargetEndpointAddress(new java.net.URL(WS_URL));
			res=(String) call.invoke(new Object[]{});
			System.out.println("receiving response:"+res);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return res;
	}
	/**
	 * 处理返回来结果
	 */
	public static void parseReceiveResponse(){
		String result=getReceiveResponse();
		String[] arr=result.split(";");
		int num=Integer.parseInt(arr[0]);
		if (num>0) {
			String content=arr[1];
			insertToDb(content);
		}
	}
	/**
	 * 插入到数据库
	 * @param xmlDoc
	 */
	public static void insertToDb(String xmlDoc) { 
        StringReader read = new StringReader(xmlDoc); 
        InputSource source = new InputSource(read); 
        SAXBuilder sb = new SAXBuilder(); 
        try { 
            Document doc = sb.build(source); 
            Element root = doc.getRootElement(); 
            List jiedian = root.getChildren(); 
            Namespace ns = root.getNamespace(); 
            Element et = null; 
            /*
            for(int i=0;i<jiedian.size();i++){ 
                et = (Element) jiedian.get(i);//循环依次得到子元素 
                UsrReport report=new UsrReport();
                String mobile=et.getChild("Phone",ns).getText();
                Linkman linkman=new Linkman();
                linkman.setLmId(mobile);
                RealDao rd=CdaoFactory.getRealDao();
                linkman=(Linkman) rd.findOne(linkman);
                report.setReMan(linkman.getLmName());
                String str=et.getChild("Time",ns).getText();
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                report.setReTime(sdf.parse(str));
                String content=et.getChild("Info",ns).getText();
                String[] arr=content.split(",");
                //短信长度为4，格式:类型,镇区,具体地点,情况
                if (arr.length==4) {
                	report.setReType(arr[0]);
                	report.setReTown(arr[1]);
                	report.setReArea(arr[2]);
                	report.setReStu(arr[3]);
                	report.setReSrc("sms");
                	report.setReStatus(0);
                	RealDao rd2=CdaoFactory.getRealDao();
                	rd2.save(report);
                	System.out.println("插入成功!");
				}else{
					System.out.println("短信格式不正确!");
				}
            } 
            */
        } catch (JDOMException e) { 
            e.printStackTrace(); 
		} catch (IOException e) {
			e.printStackTrace();
		} 
    } 
	public static void main(String[] args) {
		mySendSMS("15913394808","test");
	}
}
